//
//  DeviceCompleteImgCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/17.
//  Copyright © 2019年 East. All rights reserved.
//

#import "DeviceCompleteImgCell.h"
#import "EFANoticePubCCell.h"
#import "EFAModel.h"
#import <UIImageView+WebCache.h>

@interface DeviceCompleteImgCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImage *defalutImg;
@end
static NSString *const kReuseCellId = @"EFANoticePubCCell";
@implementation DeviceCompleteImgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

-(void) initView{
    
    _deviceImgTitle = [[UILabel alloc]init];//竣工图
    _deviceImgTitle.font = [UIFont systemFontOfSize:14.f];
    _deviceImgTitle.textColor = ColorFromRGB(0x333333);
    _deviceImgTitle.text= @"设备竣工图";
    
    _noDeviceImgText = [[UILabel alloc]init];//竣工图
    _noDeviceImgText.font = [UIFont systemFontOfSize:14.f];
    _noDeviceImgText.textColor = ColorFromRGB(0x333333);
    _noDeviceImgText.textAlignment = NSTextAlignmentCenter;
    _noDeviceImgText.hidden = YES;
    
     _layout = [[UICollectionViewFlowLayout alloc] init];
    /**
     创建collectionView
     */
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:_layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    /**
     注册item和区头视图、区尾视图
     */
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:kReuseCellId];
//    [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
//    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyCollectionViewHeaderView"];
//    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyCollectionViewFooterView"];
    
    
    
    [self.contentView sd_addSubviews:@[_deviceImgTitle,_noDeviceImgText,_collectionView]];
    
    
    _deviceImgTitle.sd_layout.heightIs(14)
    .widthIs(72)
    .topSpaceToView(self.contentView,10)
    .leftSpaceToView(self.contentView, 12);
    
    _noDeviceImgText.sd_layout.heightIs(14)
    .leftSpaceToView(self.contentView, 12)
    .rightSpaceToView(self.contentView, 12)
    .topSpaceToView(_deviceImgTitle,10);
    
    _collectionView.sd_layout
    .topSpaceToView(_deviceImgTitle, 10)
    .leftSpaceToView(self.contentView, 12)
    .rightSpaceToView(self.contentView, 12);
    
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self setupAutoHeightWithBottomView:_collectionView bottomMargin:0];
    
}

#pragma mark - UICollectionViewDataSource
/**
 分区个数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
/**
 每个分区item的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArray.count;
}
/**
 创建cell
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EFANoticePubCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuseCellId forIndexPath:indexPath];
    cell.closeBtn.hidden = YES;
    cell.contentImgView.contentMode = UIViewContentModeScaleAspectFill;
    if (indexPath.row < self.imgArray.count) {
        NSString *urlStr = self.imgArray[indexPath.row];
        [cell.contentImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]
                               placeholderImage:self.defalutImg
                                      completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                                          ;
                                      }];
    }
    return cell;
}



- (void)configViewWithImgArray:(NSMutableArray *)imgArray {
    
    
        if(imgArray && imgArray.count > 0){
            _noDeviceImgText.hidden = YES;
            self.collectionView.hidden = NO;
            self.imgArray = imgArray;
            NSInteger row = imgArray.count / 3 + ((imgArray.count % 3) ? 1 : 0);
            CGFloat cellWidth = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
            CGFloat height = cellWidth * row + (row - 1) * 15;
            if (!row) {
                height = 0;
            }
            //    self.collectionViewHeight.constant = height;
            _collectionView.height = height+34;
            self.collectionView.scrollEnabled = NO;
             [self.collectionView reloadData];
        }else{
            self.collectionView.hidden = YES;
            _noDeviceImgText.hidden = NO;
            _noDeviceImgText.text = @"暂无图片，请管理员于【设备管理】中添加。";
        }
    
    
    
   
    
}



/**
 创建区头视图和区尾视图
 */
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if (kind == UICollectionElementKindSectionHeader){
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyCollectionViewHeaderView" forIndexPath:indexPath];
//        headerView.backgroundColor = [UIColor yellowColor];
//        UILabel *titleLabel = [[UILabel alloc]initWithFrame:headerView.bounds];
//        titleLabel.text = [NSString stringWithFormat:@"第%ld个分区的区头",indexPath.section];
//        [headerView addSubview:titleLabel];
//        return headerView;
//    }else if(kind == UICollectionElementKindSectionFooter){
//        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyCollectionViewFooterView" forIndexPath:indexPath];
//        footerView.backgroundColor = [UIColor blueColor];
//        UILabel *titleLabel = [[UILabel alloc]initWithFrame:footerView.bounds];
//        titleLabel.text = [NSString stringWithFormat:@"第%ld个分区的区尾",indexPath.section];
//        [footerView addSubview:titleLabel];
//        return footerView;
//    }
//    return nil;
//
//
//}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
    return CGSizeMake(width, width);
}
/**
 点击某个cell
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(devInfoImgTCell:imgViewTapped:indexPath:)]) {
        NSMutableArray *imgViewMArray = [NSMutableArray arrayWithCapacity:self.imgArray.count];
        for (NSInteger i = 0; i < self.imgArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            EFANoticePubCCell *cell = (EFANoticePubCCell *)[collectionView cellForItemAtIndexPath:indexPath];
            if (cell) {
                [imgViewMArray addObject:cell.contentImgView];
            }
        }
        [self.delegate devInfoImgTCell:self imgViewTapped:imgViewMArray indexPath:indexPath];
    }
}
/**
 cell的大小
 */
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat itemW = (ScreenWidth-(kLineNum+1)*kLineSpacing)/kLineNum-0.001;
//    return CGSizeMake(itemW, itemW);
//}
/**
 每个分区的内边距（上左下右）
 */
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    return UIEdgeInsetsMake(kLineSpacing, kLineSpacing, kLineSpacing, kLineSpacing);
//}
/**
 分区内cell之间的最小行间距
 */
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return kLineSpacing;
//}
/**
 分区内cell之间的最小列间距
 */
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return kLineSpacing;
//}
/**
 区头大小
 */
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(kScreenWidth, 65);
//}
/**
 区尾大小
 */
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    return CGSizeMake(kScreenWidth, 65);
//}
@end
